<%	
    # Create the helper
  	flex_scaffold_helper = FlexScaffoldHelper.new("#{actions.first.camelcase}")

-%>
require 'weborb/context'
require 'rbconfig'

class <%= actions.first.camelcase %>Service

<%
  all_include_list = Array.new
  by_id_include_list = Array.new
  
  #find_all may need to include has_many and habtm classes
	(flex_scaffold_helper.has_many_associations + flex_scaffold_helper.habtm_associations).each do | assoc |
	  #Don't add these functions if it's a has_many through relationship
	  if !assoc.options.key?(:through)
	    by_id_include_list << (":" + assoc.name.to_s)
    end # not has_many :through
  end #has_many_associations + habtm_associations.each

  if !flex_scaffold_helper.belongs_to_class_names.nil?
		flex_scaffold_helper.belongs_to_class_names.values.each do |belongs_to_assoc|
		  by_id_include_list << (":" + belongs_to_assoc.name.to_s)
		  all_include_list << (":" + belongs_to_assoc.name.to_s)
		end #each belongs_to name
	end # if any belongs to
%>
  
  # Find and return all <%= flex_scaffold_helper.title.pluralize %>
  def find_all()
    <%
      #If the by_id_include_list has anything in it, drop it in place, otherwise, do a simple find_all
      if !by_id_include_list.empty? -%>
     <%= actions.first.camelcase %>.find(:all, :include => [ <%=all_include_list.join(', ') %> ])
     <%- else -%>
     <%= actions.first.camelcase %>.find(:all)
     <%- end -%>
  end

  # Find and return one <%= flex_scaffold_helper.title %>
  def find_by_id(id)
    <%
      #If the by_id_include_list has anything in it, drop it in place, otherwise, do a simple find_all
      if !by_id_include_list.empty? -%>
     <%= actions.first.camelcase %>.find_by_id(id, :include => [ <%=by_id_include_list.join(', ') %> ])
     <%- else -%>
     <%= actions.first.camelcase %>.find_by_id(id)
     <%- end -%>
  end

  # Save and return the <%= flex_scaffold_helper.source %>
  def save(<%= flex_scaffold_helper.source %>)   
    <%= flex_scaffold_helper.source %>.save
    return <%= flex_scaffold_helper.source %>
  end
  
  # Destroy the <%= flex_scaffold_helper.source %> and return the ids that were deleted
  def destroy(id)
    <%= actions.first.camelcase %>.delete(id)
    
    #Return an array of the deleted items
    return [id]
  end

end